// pages/personalCenter/receivingAddress/receivingAddressList/receivingAddressList.js
var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList : [], //地址列表数据
    selectDefault : '', //默认选中的地址Id
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.addressConheight();
    var token = wx.getStorageSync('token');       //用户的token信息
    that.setData({
      token:token,
    });

    that.loadData(token,that);
  },
  /***
   * 获取列表数据
   */
  loadData: function (token,that) {

    wx.request({
      url: apiHost + '/mobile/users/address/addrlist',
      data: {
        "token": token,
        "appId": "xcx",
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        console.log(res);
        var addressList = res.data.data;
        for(var i = 0;i< addressList.length;i++){
          if (addressList[i].isDefaultAddress == 1){
            that.setData({
              selectDefault : addressList[i].id,
            })
          }
        }
        that.selectDefault = res.data.data.id;
        that.setData({
          addressList : addressList,
        })
      }
    })
  },
  /**
   * 点击设置默认
   */
  radio:function(res){

    var that = this;
    var idflag = res.currentTarget.dataset.idflag;
    console.log(idflag);
    console.log('default',that.data.selectDefault);
    if(idflag == that.data.selectDefault){
     wx.showToast({
       title: '已经是默认地址了',
       icon:'success',
     })
    }else{
      wx.showModal({
        title: '提示',
        content: '设置该地址为默认地址吗？',
        success:function(res){
          if (res.confirm) {
            console.log('用户点击确定')
            //调用设置默认地址接口
            that.setDefaultAddress(idflag);
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  /**
   * 设置为默认地址
   */
  setDefaultAddress:function(idflag){

    var that = this;
    wx.request({
      url: apiHost + '/mobile/users/address/addDefaultAddr',
      data:{
        'token':that.data.token,
        'appId':'xcx',
        'addrId':idflag,
      },
      success:function(res){
        var code = res.data.code;
        console.log(code);
        if(code == '10000'){
          wx.showToast({
            title: '设置成功',
          }),
          //重新请求数据
          that.loadData(that.data.token,that);
        }
      },
    })
  },
  /**
   * 计算高度
   */
  addressConheight: function () {
    var that = this;

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          addressConheight: res.windowHeight - 59
        });
      }
    });
  },
/**
 * 编辑地址
 */
  addressEdit:function(res){
    var that = this;
    let idflag = JSON.stringify(res.currentTarget.dataset.idflag);
    wx.navigateTo({
      url: "../receivingEditOrAdd/receivingEditOrAdd?selectAddress="+idflag,
    })
  },
/**
 * 新建地址
 */
  goAddReceiving:function(){
    wx.navigateTo({
      url: "../receivingEditOrAdd/receivingEditOrAdd",
    })
  },
/**
 * 点击选择地址（确认订单的时候用）
 */
  chooseAddress:function(e){
    var that = this;
    var index = e.currentTarget.dataset.idflag;
    var address = JSON.stringify(that.data.addressList[index]);
    wx.navigateTo({
      url: "../../../order/fillOrder/fillOrder?address=" + address,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.addressConheight();
    var token = wx.getStorageSync('token');       //用户的token信息
    that.setData({
      token: token,
    });

    that.loadData(token, that);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})